package com.fm.financemanage.service;

import com.fm.financemanage.constant.ResponseEnum;
import com.fm.financemanage.constant.UserConstants;
import com.fm.financemanage.exception.BusinessException;
import com.fm.financemanage.mapper.AccountantMapper;
import com.fm.financemanage.mapper.BusinessEquipmentMapper;
import com.fm.financemanage.mapper.SysUserMapper;
import com.fm.financemanage.model.po.Accountant;
import com.fm.financemanage.model.po.SysUser;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.SysUserVo;
import com.fm.financemanage.model.vo.accountant.AccountantListVo;
import com.fm.financemanage.model.vo.accountant.AccountantOperatorVo;
import com.fm.financemanage.utils.TextUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class AccountantService {

    @Resource
    private AccountantMapper accountantMapper;

    @Resource
    private BusinessEquipmentMapper businessEquipmentMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    public PageVo<List<AccountantListVo>> getList(String uuid, Integer simId, String name, String identityCard, Integer page, Integer limit) {

        return new PageVo<>(accountantMapper.getList(null,uuid, simId, identityCard, name, (page - 1) * limit, limit),
                accountantMapper.count(null,uuid, simId, identityCard, name));

    }


    public void operator(AccountantOperatorVo accountantOperatorVo) {
        final Accountant accountant = new Accountant();
        BeanUtils.copyProperties(accountantOperatorVo, accountant);
        if (accountant.getId() == null) {
            accountant.setUuid(UUID.randomUUID().toString().replace("-", ""));
            accountant.setCreateTime(LocalDateTime.now());
            accountant.setUpdateTime(LocalDateTime.now());
            accountantMapper.insertSelective(accountant);
        } else {
            accountant.setUpdateTime(LocalDateTime.now());
            accountantMapper.updateByPrimaryKeySelective(accountant);
        }
    }


    public void removeById(String uuid) {
        accountantMapper.deleteByUuid(uuid);
    }

    public SysUserVo findByName(String loginName, String companyKey) {
        final SysUser byKey = sysUserMapper.findByKey(companyKey);
        if (byKey == null) {
            throw new BusinessException(ResponseEnum.COMPANY_KEY_EMPTY.getName());
        }
        Accountant accountant = accountantMapper.findByNameAndCompanyKey(loginName, byKey.getId());
        final SysUserVo sysUserVo = new SysUserVo();
        sysUserVo.setId(Long.parseLong(accountant.getId() + ""));
        sysUserVo.setUserName(loginName);
        sysUserVo.setUserType(UserConstants.USER_TYPE_KJ);
        sysUserVo.setCompanyKey(companyKey);
        sysUserVo.setPassword(accountant.getPassword());
        return sysUserVo;
    }

    public void bindSim(String accountantUuid, String simCardUuid) {
        if (TextUtil.isEmpty(simCardUuid)) {
            accountantMapper.cancelSimCard(accountantUuid);
            return;
        }
        Integer count = accountantMapper.countBySimCard(simCardUuid);
        if (count > 0) {
            throw new BusinessException("当前sim卡已被绑定不可以重复绑定");
        }
        accountantMapper.bindSimCard(accountantUuid, simCardUuid);

    }
}
